package book_two;

import javax.lang.model.element.NestingKind;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Locale;
import java.util.Objects;

class Name implements Cloneable {
    String new_name;

    public Name(String new_name) {
        this.new_name = new_name;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public boolean equals(Object obj) {//重写这个方法
        if (!(obj instanceof Name)) {//判断能否向下转型
            return false;
        }
        Name name = (Name) obj;//向下转型来调用子类进行比较
        return this.new_name.equals(name.new_name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(new_name);
    }

}
//cloneable
class clone_this implements Cloneable{
    String name;
    String age;

    Name new_name;


    public clone_this(String name, String age) {
        this.name = name;
        this.age = age;
        new_name = new Name("default");
    }

    @Override//重写clone方法
    protected Object clone() throws CloneNotSupportedException {
        //return super.clone();
        //深拷贝
        clone_this tmp = (clone_this) super.clone();//先克隆这个类的clone方法
        tmp.new_name = (Name) this.new_name.clone();//在克隆new_name这个类
        return tmp;//最后返回克隆好的类
    }
    @Override
    public String toString() {
        return "clone_this{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }
}
class Students implements Comparator<Students> {
    String name;
    int age;
    Name new_name;

    public Students(String name, int age) {
        this.name = name;
        this.age = age;
        new_name = new Name("null");
    }

//    public int compareTo(Students o) {
//        if (o.name.equals(this.name)) {
//            return 1;
//        } else {
//            return -1;
//        }
//
//    }

    @Override
    public String toString() {
        return "Students{" +
                "name='" + name + '\'' +
                ", age='" + age + '\'' +
                '}';
    }

    @Override
    public int compare(Students o1, Students o2) {
        return o1.age-o2.age;
    }
}

class Other implements Comparator<Students> {
    @Override
    public int compare(Students o1, Students o2) {
        return o2.age-o1.age;//降序
    }
}

public class shili {
    static void text_obj(Object obj) {//这样不管什么类能够接收2

        if (obj instanceof clone_this) {//向下转型调用clone_this的成员
            clone_this h = (clone_this) obj;
            System.out.println("text_obj show " + h.toString());
        }
        if (obj instanceof Students) {
            Students h = (Students) obj;
            System.out.println(h.toString());
        }
    }




    public static void maain(String[] args) throws CloneNotSupportedException {
        clone_this h = new clone_this("hadage", "19");
        clone_this hh = (clone_this) h.clone();
        clone_this hhh = (clone_this) h.clone();
        System.out.println(h.toString());
        h.new_name.new_name = "h";//跟改了这个类的成员名字
        System.out.println(h.new_name.new_name);
        System.out.println(hh.toString());
        System.out.println(hh.new_name.new_name);
        hhh.new_name.new_name = "hhh";//跟改了这个类的成员名字
        System.out.println(hhh.toString());
        System.out.println(hhh.new_name.new_name);
        text_obj(h);
    }

    public static void mainm(String[] args) {
        Students[] class1 = {new Students("wanger", 22),
                new Students("hhh", 19),
                new Students("hadage", 19)};
        Other other = new Other();
        Arrays.sort(class1, other);
        System.out.println(Arrays.toString(class1));
    }
}

